function gadget:GetInfo()
	return {
		name = "Knight",
		desc = "Knight abilites and upgrades", -- need to ensure will run for multiple Knights as well...
		author = "Sean Heron",
		date = "2. January 2009",
		license = "GNU GPL v2, or later",
		layer = 29, -- does it need to be the same layer so that they don't set levelup back for each other? or easiest just make one global for general and one for each Engines script? probably go with option two there
		enabled = true
	}
end

local debugon = false

local knight=UnitDefNames.knight.id

local CMD_RAMPAGE = 32299


local CMD_UPRAMPAGELVL = 33335
local CMD_UPJUMPLVL = 33336

local rampageLvl	= {}	-- this stays here of course
local jumpLvl	= {}
local rampageLvlmax = 3
local jumpLvlmax = 3	-- these should be config - need to think about config structure (ie one config file for each Engine ? or one for all engines ?
						-- also potentially a table, and the max level dependent on which Unit is using/has ability (if not even go for per unit level definitions (oh that would come into play elsewhere though)
local engineLevel = {}		-- some stuff works without.. strange but true..
local specificLevelUp = {}

--local LvlupChoiceActive = {}
local removeLvlupChoice = {}
local lvlupsRemaining = {}

local teamswithKnight = {}

-- local jumpData = {} -- this is wrong - we're defining it below -- actually need to define here so valid for entire gadget - but set below

if (gadgetHandler:IsSyncedCode()) then

--SYNCED

--local function LevelTwoFunction()	-- use this so don't need same code in UnitCreated and GameFrame
--end


-------Is currently here as well as in AbilityRampage.lua because of insertion on "leveling" ------
---------------clarify more generally later--------------------------------------------------------
		-- could be a global ? or config and include? ---
local rampageDesc = {		-- why have this here and not outside synced ? So that it's clear where it's used? good practice to have at lower level? or just KDR quirk..
	name="Rampage",
	tooltip="higher rate of attack, and speed increase",
	action="rampage",
	id=CMD_RAMPAGE,
	type=CMDTYPE.ICON,	-- this might fuck me about... look at cursed actually morph has somewhat similiar - but only somewhat...
	-- cursor="Unload units",	-- no cursor, as no map command
}


--------these definitely stay here ---------------------------------
local upRampageLvl = {
	name="Up Rampage Lvl",
	tooltip="higher rate of attack, (and speed increase - soon :P)",
	action="Ramplevelup",
	id=CMD_UPRAMPAGELVL,
	type=CMDTYPE.ICON,
}

local upJumpLvl = {
	name="Up Jump Lvl",
	tooltip="increased jump range, and speed increase",
	action="Jumplevelup",
	id=CMD_UPJUMPLVL,
	type=CMDTYPE.ICON,
}

local function AddAbilityLevelUp(unitID, uniqueID) -- teamID is equivalent to the unique Engine identifiere (maybe EngineID ?)

	if lvlupsRemaining[uniqueID] < 1 then -- hmm, there's other ways (perhaps better) this could be done - but should do the trick for now
		-- okay - what we still don't have is checking the current Abilitylevel vs overall lvl (to limit repeatedly leveling just one ability)
		if rampageLvl[uniqueID] < rampageLvlmax then-- not yet at the rampagemax
			Spring.InsertUnitCmdDesc(unitID,upRampageLvl)
		end
		if jumpLvl[uniqueID] < jumpLvlmax then
			Spring.InsertUnitCmdDesc(unitID,upJumpLvl)
		end
	end

end

local function RemoveAbilityLevelUp(unitID, CMD)

		local cmdID = Spring.FindUnitCmdDesc(unitID, CMD)
		if cmdID ~= nil then
			Spring.RemoveUnitCmdDesc(unitID, cmdID)
		end
end

function gadget:UnitCreated(unitID, unitDefID, teamID)

	-- if unitDefID == knight then

		-- if rampageLvl[teamID] and rampageLvl[teamID] >= 1 then	-- insert rampage button on respawn
			-- Spring.InsertUnitCmdDesc(unitID,rampageDesc)		-- why was this commented out ? oh - cause I'd put it into AbilityRampage, but it wasn't working there...
		-- end
		-- -- need to readd the upgrade selections here as well if someone died before choosing
	-- end
end

function gadget:UnitDestroyed(unitID, unitDefID, teamID, attackerID)
	-- question is, whether adding cmds here is allowed.
	-- Could check in unit destroyed... but that would be in parallel to the var being set, no ? Prob just do every 5 gameframes...
								-- hmm, if do layer right, the "unit destroyed" way might be a good way of doing - since that will be (usually) less checks then every 5 frames, and is immediately after the event.
end

function gadget:Initialize()
	engineLevel = GG.engineLevel
	specificLevelUp = GG.specLevelup

	rampageLvl = GG.rampageLvl
	jumpLvl = GG.jumpLvl
				-- -- need to switch were it's defined, and where the global is imported ? - actually, don't need jumpData at all here :) !
							-- -- ah, it already was table in table - so want table in table in table for greatest flexibilty :P ?
	-- jumpData = GG.jumpData -- no need to just load Knight specific - or would that even work ? (could do that to reduce Lua load a bit if it does)


end

function gadget:GameFrame(f)
	if f==10 then -- Engines currently spawned at frame 4
		local teams = Spring.GetTeamList()
		for _,teamID in ipairs(teams) do	-- want to do a different system than this - universal UniqueIDs for Engines (/upgradeable  Units)
			if Spring.GetTeamUnitDefCount(teamID, knight) > 0 then
				teamswithKnight[teamID] = true	--add his team to list of teams/ie players with knight - this to be changed when we change to a more universal Engine tracking system
				rampageLvl[teamID] = 0
				jumpLvl[teamID]	= 1
				lvlupsRemaining[teamID] = 0
				removeLvlupChoice[teamID] = 0	-- zero works as "false" using this to pass on the unitID while we're at it

			end
		end
	end

	if f%5 < .1 then -- every 5 frames (30 or 32 frames == 1 second)
		for teamID, _ in pairs(teamswithKnight) do
			if specificLevelUp[teamID] ~= 0 then -- can use the unitID which is passed on from experience.lua (yes, the variable name doesn't exactly indicate that...) - for now

				if engineLevel[teamID] >= 2 and engineLevel[teamID] < 7 then
					AddAbilityLevelUp(specificLevelUp[teamID], teamID)
					lvlupsRemaining[teamID] = lvlupsRemaining[teamID] + 1
				end
				specificLevelUp[teamID] = 0		-- set back to zero so that it can be upped again (conviently to unitID) when another lvlup occurs
			end


			if removeLvlupChoice[teamID] ~= 0 then

				RemoveAbilityLevelUp(removeLvlupChoice[teamID], CMD_UPJUMPLVL)
				RemoveAbilityLevelUp(removeLvlupChoice[teamID], CMD_UPRAMPAGELVL)

				removeLvlupChoice[teamID] = 0				-- maybe - it doesn't want the cmdID ?
			end
		end


	end

end



function gadget:CommandFallback(unitID, unitDefID, teamID, cmdID, cmdParams, cmdOptions, cmdTag)

	if cmdID == CMD_UPJUMPLVL and removeLvlupChoice[teamID] == 0 then -- the later part for catching any repeated clicks before the commands are removed
		-- up the jump level!
		if 	jumpLvl[teamID] == 1 then

			Spring.Echo("JumpJets improved! Level 2")

			jumpLvl[teamID] = 2

		elseif jumpLvl[teamID] == 2 then

			jumpLvl[teamID] = 3
			Spring.Echo("Jump power increased! Level 3")

		end		-- shouldn't be a need for a catchall - after all, the cmddesc shouldn't be inserted if its maxed level

		lvlupsRemaining[teamID] = lvlupsRemaining[teamID] - 1
		if lvlupsRemaining[teamID] == 0 then
			removeLvlupChoice[teamID] = unitID -- a value other than zero counts as "true" - passing on the ID of the unit that needs the upgrade commands removed while we're at it
		end
		return true, true
	end

	if cmdID == CMD_UPRAMPAGELVL and removeLvlupChoice[teamID] == 0 then
		-- up the rampage level!
		if rampageLvl[teamID] == 0 then
			Spring.Echo("rampagelvlup being called ?")
			rampageLvl[teamID] = 1

			-------------------------------------
			--------NEEDS TO BE SOLVED!!----------
			Spring.InsertUnitCmdDesc(unitID,rampageDesc)
				-- hmm - I moved the other InsertCmd Descriptions...
				-- this is going to be a general problem though, with abilities coming after Unitcreation
				-- could just add to Rampage as well -hmm, both solutions seem messy in some ways
			-----Question is really how best to intergrate with Gui------
			----------------------------------------

		elseif rampageLvl[teamID] >= 1 and rampageLvl[teamID] < rampageLvlmax then	-- shouldn't need the checkagains rampageLvlmax, since the order shouldn't be added if we already are at max - but doesn't hurt to have here forthemoment
			rampageLvl[teamID] = rampageLvl[teamID] + 1
		end		-- 	again - shouldn't need catchall - ah.. but do if I don't have checks up there (but better there than here - to not show the command if it can't be upped)

		-- set var to remove the upabilitylvl cmd-descriptions at a different place
		lvlupsRemaining[teamID] = lvlupsRemaining[teamID] - 1
		if lvlupsRemaining[teamID] == 0 then
			removeLvlupChoice[teamID] = unitID -- this needs to be adapted / checked for if you have any levelups left - here or in the remove bit ?
		end
			-- It doesn't like removing the command from here, while still in "CommandFallback".. So had to move the remove bit to gameframe. Oh - update would work as well, yes ? If command descriptions can be messed with from there. Would be neater, cause then would remove as fast as possible, but without performance impact!
		return true, true
	end

	return false
end



else
--UNSYNCED


function gadget:Update()
end

end